/**
 * Copyright (c) 2020 libinbin000
 * SFrame is licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 *     http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 */


import {PlayerData} from "./PlayerData";
import {Connect} from "./Connect";


export class Player{
    static playerList = new Map<number, Player>();

    static getPlayerByID(uid, callback:(player:Player)=>void):void{
        if(this.playerList.has(uid)){
            callback(this.playerList.get(uid));
        }else{
            let player = new Player(uid);
            this.playerList.set(uid, player);
            player.loadData(()=>{
                callback(player);
            });
        }
    }

    public readonly uid:number;         //用户id
    public readonly data:PlayerData;    //用户数据
    private connect:Connect;

    constructor(uid:number){
        this.uid = uid;
        this.data = new PlayerData();
    }

    private loadData(callback:Function){
        //从数据库加载用户数据
        this.data.load(this.uid, ()=>{
            callback();
        });
    }

    public sendCmd(msgID:number, data:any){
        this.connect.sendCmd(msgID, data);
    }

    public setConnect(connect:Connect){
        this.connect = connect;
    }
}